package com.yangyouli.unity_demo_server.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.yangyouli.unity_demo_server.annotation.Inject;
import com.yangyouli.unity_demo_server.annotation.ResponseClass;
import com.yangyouli.unity_demo_server.annotation.ResponseMethod;
import com.yangyouli.unity_demo_server.entity.Friends;
import com.yangyouli.unity_demo_server.entity.FriendsVO;
import com.yangyouli.unity_demo_server.server.Client;
import com.yangyouli.unity_demo_server.server.DataSave;
import com.yangyouli.unity_demo_server.server.serializeModel.FriendsApplication;
import com.yangyouli.unity_demo_server.server.serializeModel.FriendsApplicationVO;
import com.yangyouli.unity_demo_server.service.FriendsService;
import com.yangyouli.unity_demo_server.util.ThreadLocalHelper;

@ResponseClass("friends")
public class FriendsController {
	/**
	 * 客户端对应的相应类名，玩家在线收到申请好友消息时，通知他刷新消息列表，发送数据1表示有新消息，0则没有
	 */
	static final String REFLUSH_NEWS_RESPONSE_CLASS_NAME = "HasNewAddFriendsApplicationRequest";
	
	@Inject
	private FriendsService friendsService;
	
	/**
	 * 发送添加好友请求，参数为 FriendsApplication 的 json 字符串
	 * @param data -1 不能添加自己为好友，0 已经是好友了 ，1 申请已发送
	 */
	@ResponseMethod("addApplication")
	public String addFriendsApplication (String data) {
		FriendsApplication fa = JSONObject.parseObject(data, FriendsApplication.class);
		if (fa.getRespondentUserId() == ThreadLocalHelper.getClient().getUserId()) {
			return "-1"; // 不能添加自己为好友
		}
		fa.setApplicationTime(new Date().getTime());
		fa.setExpireTime(DataSave.APPLICATION_EXPIRE_TIME);
		Friends f = new Friends();
		f.setUserId1(fa.getApplicantUserId());
		f.setUserId2(fa.getRespondentUserId());
		if (friendsService.find(f) == null) {
			DataSave.getInstance().addFriendsApplicationMessage(fa);
			Client responseUser = ThreadLocalHelper.getClient().getServer().getLoginUserClient(fa.getRespondentUserId());
			if (responseUser != null) {
				// 玩家在线，通知其刷新消息列表
				responseUser.sendResponse(REFLUSH_NEWS_RESPONSE_CLASS_NAME, "1");
			}
			return "1";
		} else { // 已经是好友了
			return "0";
		}
	}
	
	/**
	 * 判断该用户是否有未读的好友请求消息
	 * @param data
	 * @return 1 有新的消息， 0 没有新的消息
	 */
	@ResponseMethod("hasNotReadNews")
	public String hasNotReadNews (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		List<FriendsApplication> faList = DataSave.getInstance().getFriendsApplicationMessage(userId);
		if (faList != null) {
			for (FriendsApplication fa : faList) {
				if (!fa.isRead()) {
					return "1";
				}
			}
		}
		return "0";
	}
	
	/**
	 * 获取特定用户的好友申请请求列表
	 * @param data
	 */
	@ResponseMethod("getFriendsApplication")
	public String getFriendsApplication (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		List<FriendsApplication> faList = DataSave.getInstance().getFriendsApplicationMessage(userId);
		List<FriendsApplicationVO> faVOList = new ArrayList<>();
		if (faList != null) {
			for (FriendsApplication f : faList) {
				FriendsApplicationVO fav = new FriendsApplicationVO();
				fav.SetParentValue(f);
				FriendsVO fvo = friendsService.findByUserId(f.getApplicantUserId());
				fav.setFriendsVO(fvo);
				faVOList.add(fav);
			}
		}
		return JSONObject.toJSONString(faVOList);
	}
	
	/**
	 * 将特定用户的好友申请请求消息设为已读
	 * @param data
	 */
	@ResponseMethod("readFriendsApplication")
	public void readFriendsApplication (String data) {
		int userId = ThreadLocalHelper.getClient().getUserId();
		List<FriendsApplication> faList = DataSave.getInstance().getFriendsApplicationMessage(userId);
		if (faList != null) {
			for (FriendsApplication f : faList) {
				f.setRead(true);
			}
		}
	}
	
	/**
	 * 添加好友，参数为 Friends json 字符串
	 * -1 已和该用户为好友，返回 0 失败， 1 成功
	 * @param data
	 */
	@ResponseMethod("add")
	public String addFriends (String data) {
		Friends f = JSONObject.parseObject(data, Friends.class);
		// 将消息删除
		DataSave dataSave = DataSave.getInstance();
		dataSave.removeFriendsApplicationMessage(dataSave.getFriendsApplication(f.getUserId1(), f.getUserId2()));
		return "" + friendsService.addFriends(f);
	}
	
	@ResponseMethod("refuseAddFriend")
	public void refuseAddFriends (String data) {
		Friends f = JSONObject.parseObject(data, Friends.class);
		// 拒绝添加好友，将消息删除
		DataSave dataSave = DataSave.getInstance();
		dataSave.removeFriendsApplicationMessage(dataSave.getFriendsApplication(f.getUserId1(), f.getUserId2()));
	}
	
	/**
	 * 删除好友，参数为 Friends json 字符串
	 * @param data
	 * @return 0 和该玩家已经不是好友，1 删除成功
	 */
	@ResponseMethod("delete")
	public String deleteFriends (String data) {
		Friends f = JSONObject.parseObject(data, Friends.class);
		return "" + friendsService.deleteFriends(f);
	}
	
	/**
	 * 查找所有的好友，返回值为 FriendsVO 集合的 json 字符串
	 * @param data
	 * @return
	 */
	@ResponseMethod("find")
	public String findFriends (String data) {
		Client c = ThreadLocalHelper.getClient();
		int userId = c.getUserId();
		List<FriendsVO> fv = friendsService.findFriends(userId);
		List<FriendsVO> onlineUser = new ArrayList<>();
		List<FriendsVO> offlineUser = new ArrayList<>();
		for (FriendsVO f : fv) {
			if (c.getServer().isUserOnline(f.getFriendsId())) {
				f.setLogin(true);
				onlineUser.add(f);
			} else {
				offlineUser.add(f);
			}
		}
		onlineUser.sort(FriendsVO.DESC_SORT_BY_LEVEL_COMPARATOR);
		offlineUser.sort(FriendsVO.DESC_SORT_BY_LEVEL_COMPARATOR);
		fv = new ArrayList<>();
		fv.addAll(onlineUser);
		fv.addAll(offlineUser);
		return JSONObject.toJSONString(fv);
	}
}
